﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Utility;
using System.IO;

namespace WebHoiChoMuaBan.Controllers.Private
{
    public class AdvertisementController : PrivateController
    {
        //
        // GET: /Advertisement/

        private void Init()
        {
            ViewData["mid"] = "ad";
            LoadTimeAndCurrency();
        }

        public ActionResult Index(int? uid)
        {
            int pageNum;
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                if (!int.TryParse(Request.QueryString["p"], out pageNum))
                    pageNum = 1;
                AdvertHelper adHelper = new AdvertHelper(uid.Value);
                ViewData["ListAdvertisement"] = adHelper.getListAdvertisement(pageNum);
                //Dùng để truyền qua trang liệt kê danh sách các quảng cáo
                ViewData["MemId"] = uid.Value;
                return View("~/Views/Private/Advertisement.aspx");
            }
            return HomePage();
        }

        [HttpPost]
        public ActionResult Add(FormCollection data, HttpPostedFileBase imgAdvert)
        {
            int uid = data.Get("MemId") == null ? uid = 0 : int.Parse(data.Get("MemId"));
            if (uid == 0)
                return HomePage();
            Advertisment advert = new Advertisment();
            AdvertHelper advertHelper = new AdvertHelper();
            if (LoadMember(uid) == null)
            {
                Init();
                advert.MemId = uid;
                advert.AdTitle = data.Get("txtTitle");
                advert.AdLink = data.Get("txtLink");
                advert.AdPrice = data.Get("txtPrice") == "" ? 0 : decimal.Parse(data.Get("txtPrice"));
                advert.AdContent = data.Get("txtContent");
                advert.AdThumb = imgAdvert == null ? "" : imgAdvert.FileName;
                advert.AdPostDate = DateTime.Now;
                ImageHelper imgHelper = new ImageHelper();
                if (imgHelper.CheckInfoIcon(imgAdvert))
                {
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), "thumbnail_" + imgAdvert.FileName);
                    imgHelper.uploadImage(path, imgAdvert, 200, 200);
                    if (advert.IsValid)
                    {
                        advertHelper.Add(advert);
                        advertHelper.Save();
                        return RedirectToAction("Index", "Advertisement", new { uid = uid });
                    }
                    else
                    {
                        foreach (var issue in advert.GetRuleViolations())
                        {
                            ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                        }
                        //Nếu có lỗi thì chuyển lại trang List quảng cáo để show lỗi
                        ViewData["Advertisement"] = advert;
                        return Index(uid);
                    }
                }
            }
            return HomePage();
        }

        [HttpPost]
        public RedirectResult changeAdStatus(int id)
        {
            new AdvertHelper().changeAdsStatus(id);
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }
    }
}
